import { Entity, Column, ManyToMany, JoinTable } from "typeorm";
import { Base } from "@/entities.base/base.entity";
import { Permission } from "../permission.entity";
import { User } from "../user.entity";

@Entity({ comment: '角色表', name: 'sys_role' })
export class Role extends Base {

    /** 角色名称 */
    @Column({
        comment: '角色名称',
        length: 50,
        unique: true
    })
    name: string;

    /** 角色代码 */
    @Column({
        comment: '角色代码',
        length: 100,
        unique: true
    })
    code: string;

    /** 角色描述 */
    @Column({
        comment: '角色描述',
        length: 200,
        nullable: true
    })
    description: string;

    /** 角色拥有的权限 */
    @ManyToMany(() => Permission, permission => permission.roles)
    @JoinTable({
        name: 'sys_role_permission',
        joinColumn: { name: 'role_id', referencedColumnName: 'id' },
        inverseJoinColumn: { name: 'permission_id', referencedColumnName: 'id' }
    })
    permissions: Permission[];

    /** 拥有该角色的用户 */
    @ManyToMany(() => User, user => user.roles)
    users: User[];
}
   